package com.wyz.socket.test.server;


import com.wyz.socket.core.base.Context;
import com.wyz.socket.core.impl.IoSelectorProvider;
import com.wyz.socket.core.impl.scheduler.SchedulerImpl;
import com.wyz.socket.core.impl.stealing.IoStealingSelectorProvider;
import com.wyz.socket.test.Common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Server {
    public static void main(String[] args) throws IOException {
        Context.setup()
                .setIoProvider(new IoSelectorProvider())
                .setScheduler(new SchedulerImpl(1))
                .start();

        TCPServer tcpServer = new TCPServer(Common.TCP_PORT_SERVER);
        boolean isSucceed = tcpServer.start();
        if (!isSucceed) {
            System.out.println("Start TCP server failed!");
            return;
        }
        UDPProvider.open(Common.TCP_PORT_SERVER);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String str;
        do {
            str = bufferedReader.readLine();
            if(str == null  ||  str.startsWith("00bye00")){
                break;
            }
            if(str.length() == 0) continue;
            tcpServer.broadcast(str);
        } while (true);

        UDPProvider.close();
        tcpServer.close();
        Context.close();
    }

}
